﻿/*************************************************************************************
 * 文 件 名:   HK_MouseMoveAndClick
 * 
 * 描    述:   RoamingCam控制鼠标点击处罚位移的功能脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  谭潇杰、京产肠饭
 * 创建时间：  2022/1/28
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 记录鼠标的坐标点  用于点击判断
/// </summary>
public class HK_TeleportSelect : MonoBehaviour
{
    public static List<HK_Point> teleports = new List<HK_Point>();

    GameObject cursor_Go;

    // 摄像机的射线检测
    RaycastHit hit;
    bool isMouseMoving;

    void Awake()
    {
        cursor_Go = transform.Find("Cursor").gameObject;
    }

    void Update()
    {
        if (!HK_Player.Ins.IsCanRotCam)
        {
            if (cursor_Go.activeInHierarchy)
                cursor_Go.SetActive(false);

            return;
        }

        if (HK_Player.Ins.isMobile)
            TouchFollow();
        else
            MouseFollow();
    }

    Vector2 initTouchPos;
    Vector2 finalTouchPos;

    //移动端  
    void TouchFollow()
    {
        if (Input.touchCount == 1)
        {
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                return;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                if (hit.collider == null || hit.collider.tag != "Teleport")
                    return;

                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    initTouchPos = Input.GetTouch(0).position;
                }

                if (Input.GetTouch(0).phase == TouchPhase.Ended)
                {
                    finalTouchPos = Input.GetTouch(0).position;

                    if (Vector2.Distance(initTouchPos, finalTouchPos) < 10)
                        CalcDis(hit.point);
                }
            }
        }
    }

    //鼠标
    void MouseFollow()
    {
        // 如果触碰到UI 
        if (EventSystem.current.IsPointerOverGameObject())
        {
            if (cursor_Go.activeInHierarchy)
                cursor_Go.SetActive(false);

            return;
        }

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
        {
            if (hit.collider == null || hit.collider.tag != "Teleport")
            {
                if (cursor_Go.activeInHierarchy)
                    cursor_Go.SetActive(false);

                return;
            }
            else if (HK_Player.Ins.ViewMode == ViewMode.Roam)
            {
                if (!cursor_Go.activeInHierarchy)
                    cursor_Go.SetActive(true);
            }

            cursor_Go.transform.position = new Vector3(hit.point.x, hit.point.y + 0.01f, hit.point.z);

            if (Mathf.Abs(Input.GetAxis("Mouse Y")) >= 0.1f || Mathf.Abs(Input.GetAxis("Mouse X")) >= 0.1f)
                isMouseMoving = true;

            // 鼠标移动后，提起鼠标0.05s之后，切换为静止状态
            if (isMouseMoving && !Input.GetMouseButton(0))
                StartCoroutine(CancelMouseMoving_IE());

            if (Input.GetMouseButtonUp(0) && !isMouseMoving)
                CalcDis(hit.point);
        }
        else
        {
            if (cursor_Go.activeInHierarchy)
                cursor_Go.SetActive(false);
        }
    }

    IEnumerator CancelMouseMoving_IE()
    {
        yield return new WaitForSeconds(0.05f);
        isMouseMoving = false;
    }

    void CalcDis(Vector3 _hitPos)
    {
        List<float> disList = new List<float>();

        for (int i = 0; i < teleports.Count; i++)
            disList.Add(Vector3.Distance(teleports[i].TargetPos, _hitPos));

        float minDis = disList[0];
        int index = 0;

        // 求出最近距离TeleportPoint的索引
        for (int i = 0; i < disList.Count; i++)
        {
            if (disList[i] > minDis)
                continue;

            minDis = disList[i];
            index = i;
        }

        // 如果角色离最近的目标点小于一米，则不位移过去
        if (Vector3.Distance(teleports[index].TargetPos, transform.position) < 1f)
        {
            disList.Clear();
            return;
        }

        if (HK_Player.Ins.ViewMode == ViewMode.BirdView || HK_Player.Ins.ViewMode == ViewMode.ThreeD)
            HK_Player.Ins.ChangeToRoamMode(teleports[index]);
        else
            HK_Player.Ins.CameraMove2Target(teleports[index]);

        disList.Clear();
    }
}